/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gob.fonafe.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import pe.gob.fonafe.domain.BienSubastaBean;

/**
 *
 * @author czela
 */
public class MuestraImagenBiensSubastaAction extends ActionSupport implements Preparable,RequestAware{
    
    private static final Logger logger = Logger.getLogger(MuestraImagenBiensSubastaAction.class);
    private Map<String,Object> request;

    private String idBien;
    /*Params request*/
    private InputStream inputStream;
    private String mimeType;

    public void prepare() throws Exception {
        mimeType ="";
    }

    public String verFotoPrincipalBien(){
        String resultado="";
        try{
            HttpSession sesion = ServletActionContext.getRequest().getSession();
            List<BienSubastaBean> listSubastaBienes = (List<BienSubastaBean>)sesion.getAttribute("listaBienesAAsignar");
            if(!idBien.equals("")){
                byte[] fotoPrinci=null;
                for(BienSubastaBean bean: listSubastaBienes){
                    if(bean.getId().equalsIgnoreCase(idBien)){
                        fotoPrinci = bean.getFotoPrincipal();
                        break;
                    }
                }
                if(fotoPrinci != null){
                    inputStream = new ByteArrayInputStream(fotoPrinci);
                    mimeType = "image/gif";
                    resultado = SUCCESS;
                }
            }
        }catch(Exception ex){
            resultado = ERROR;
            ex.printStackTrace();
            logger.error(ex);
        }
        return resultado;
    }

    public String verFotoPrincipalBienAsociado(){
        String resultado="";
        try{
            HttpSession sesion = ServletActionContext.getRequest().getSession();
            List<BienSubastaBean> listSubastaBienes = (List<BienSubastaBean>)sesion.getAttribute("listBienesAsociados");
            if(!idBien.equals("")){
                byte[] fotoPrinci=null;
                for(BienSubastaBean bean: listSubastaBienes){
                    if(bean.getId().equalsIgnoreCase(idBien)){
                        fotoPrinci = bean.getFotoPrincipal();
                        break;
                    }
                }
                if(fotoPrinci != null){
                    inputStream = new ByteArrayInputStream(fotoPrinci);
                    mimeType = "image/gif";
                    resultado = SUCCESS;
                }
            }
        }catch(Exception ex){
            resultado = ERROR;
            ex.printStackTrace();
            logger.error(ex);
        }
        return resultado;
    }

    public void setRequest(Map<String, Object> request) {
        this.request = request;
    }

    public String getIdBien() {
        return idBien;
    }

    public void setIdBien(String idBien) {
        this.idBien = idBien;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }
    

}
